package com.gxc.datacore.metadata.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxc.datacore.metadata.domain.Dictionary;
import com.gxc.datacore.metadata.domain.Table;
import com.gxc.datacore.metadata.util.SqlDaoUtil;
import com.gxc.dbcore.dao.SqlDao;
import com.gxc.dbcore.service.BeanService;
import com.gxc.dbcore.util.PageInfo;
import com.gxc.dbcore.util.PageList;

@Service
public class DictionaryService {

	@Autowired
	private SqlDao sqlDao;
	@Autowired
	private BeanService beanService;
	@Autowired
	private TableService tableService;

	public Dictionary retrieve(String id) {
		return beanService.get(sqlDao, Dictionary.class, id);
	}

	public int create(Dictionary dictionary) {
		return beanService.create(sqlDao, dictionary);
	}

	public int update(Dictionary dictionary) {
		return beanService.update(sqlDao, dictionary);
	}

	public int delete(String id) {
		return beanService.delete(sqlDao, Dictionary.class, id);
	}

	public int delete(String[] idArray) {
		return beanService.delete(sqlDao, Dictionary.class, idArray);
	}

	@SuppressWarnings("rawtypes")
	public List list(Map params) {
		return beanService.list(sqlDao, Dictionary.class, null, null);
	}

	@SuppressWarnings("rawtypes")
	public PageList listPage(Map params, PageInfo pageInfo) {
		return beanService.listPage(sqlDao, Dictionary.class, null, null,
				pageInfo);
	}

	/**
	 * 获取字典数据
	 * 
	 * @param dictionary_id
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public List listDicData(String dictionary_id) {
		Dictionary dictionary = this.retrieve(dictionary_id);
		if (dictionary != null) {
			Table table = tableService.retrieve(dictionary.getTable_id());
			SqlDao sqlDao = SqlDaoUtil.getSqlDaoByTable(table);
			Map params = new HashMap();
			params.put("schema_name", table.getSchema_name());
			params.put("table_name", table.getTable_name());
			params.put("key_column", dictionary.getKey_column());
			params.put("value_column", dictionary.getValue_column());
			return sqlDao.selectList("datacenter.dictionary.listDicData",
					params);
		}
		return null;
	}

}
